package cn.wolfcode.trip.app.filter;


import cn.wolfcode.trip.base.util.UploadUtil;
import org.apache.commons.io.FileUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
//这里不用拦截器的原因,由于admin和app项目中的路径不同,一个是*.do,一个是 /
public class ImageFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //获取当前图片的访问路径
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI();
        //判断公共区域是否拥有该图片
        File file = new File(UploadUtil.PATH, uri);
        if (file.exists()) {
            //把图片响应给浏览器
            response.getOutputStream().write(FileUtils.readFileToByteArray(file));
        }else{
            //直接放行
            chain.doFilter(request,response);
        }

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }


    @Override
    public void destroy() {

    }
}
